package ai2;

import java.util.ArrayList;
import java.util.Scanner;

public class UI {
	
	private String fileName = "catalogRules.txt";
	
	InferenceEngine ie;
	WorkingMemory wm;
	
	public UI() {
		ie = new InferenceEngine(fileName);
		wm = ie.getWM();
		wm.addFact("Student is_a STUDENT");
		intro();
		mainLoop();
	}
	
	private void intro() {
		System.out.println("Welcome to HELL v.01");
	}
	
	private void mainLoop() {
		Scanner input = new Scanner(System.in);
		String userInput = "";
		boolean userDone = false;
		
		while (!userDone) {
			printMenu();
			System.out.print(": ");
			userInput = input.next().trim().toUpperCase();
			userDone = checkInput(userInput);
		}
	}
	
	private void printMenu() {
		System.out.println();
		System.out.println("    menu");
		System.out.println();
		System.out.println("1 / load transcript");
		System.out.println("2 / print transcript");
		System.out.println("3 / print all classes the student is eligible for");
		System.out.println("4 / check if the student is eligible for a class");
		System.out.println("5 / check if the student can graduate");
		System.out.println("6 / end this program");
		System.out.println();
	}
	
	private boolean checkInput(String input) {
		boolean userDone = false;
		if (input.equals("1")) {
			loadTranscript();
		}
		else if (input.equals("2")) {
			printTranscript();
		}
		else if (input.equals("3")) {
			printAllEligibleClasses();
		}
		else if (input.equals("4")) {
			checkIfStudentCanTakeClass2();
		}
		else if (input.equals("5")) {
			
		}
		else if (input.equals("6")) {
			System.out.println("good bye.");
			userDone = true;
		}
		return userDone;
	}
	
	private void loadTranscript() {
		ArrayList<String> transcript = new ArrayList<String>();
		Scanner input = new Scanner(System.in);
		String userInput = "";
		String regex = "[A-Z]{4}[0-9]{4}[H,K,L,U]?";
		boolean userDone = false;
		
		System.out.println("enter classes you've taken, type END to stop.");
		
		while (!userDone) {
			System.out.print(": ");
			userInput = input.next().trim().toUpperCase().replace(" ", "");
			if (userInput.matches(regex)) {
				transcript.add(userInput);
				System.out.println("added class: " + userInput);
			}
			else if (userInput.equals("END")) {
				userDone = true;
			}
			else {
				System.out.println("error: " + userInput + " does not match the required format");
			}
		}
		
		for (int i = 0; i < transcript.size(); i++) {
			String currentClass = transcript.get(i);
			String newFact = "";
			
			newFact = "Student has_taken " + currentClass;
			//System.out.println("newFact: " + newFact);
			wm.addFact(newFact);
		}
		
		wm.printFacts();
	}
	
	private void printTranscript() {
		ArrayList<String> transcript = wm.getFacts();
		System.out.println("your transcript:");
		if (transcript.isEmpty()) {
			return;
		}
		for (int i = 0; i < transcript.size(); i++) {
			System.out.println("class " + (i + 1) + " " + transcript.get(i));
		}
	}
	
	private void printAllEligibleClasses() {
		System.out.println("printing all eligible classes");
		ArrayList<String> facts = wm.getFacts();
		ie.analyze();
		for (int i = 0; i < facts.size(); i++) {
			String currentFact = facts.get(i);
			//System.out.println("currentFact: " + currentFact);
			String regex = "can_take";
			
			if (currentFact.contains(regex)) {
				System.out.println(currentFact);
			}
		}
	}
	
	private void checkIfStudentCanTakeClass() {		
		ArrayList<String> facts = wm.getFacts();
		
		Scanner input = new Scanner(System.in);
		String userInput = "";
		String regex = "[A-Z]{4}[0-9]{4}[H,K,L,U]?";
		boolean userDone = false;
		
		System.out.println("enter a class you wish to check eligibility for, type END to stop.");
		
		while (!userDone) {
			System.out.print(": ");
			userInput = input.next().trim().toUpperCase().replace(" ", "");
			if (userInput.matches(regex)) {
				String classToMatch = "Student can_take " + userInput;
				//System.out.println("classToMatch: " + classToMatch);
				boolean match = false;
				for (int i = 0; i < facts.size() && !match; i++) {
					if (facts.get(i).matches(classToMatch)) {
						System.out.println("Yes, the student is eligible to take: " + userInput);
						match = true;
					}
				}
				if (!match) {
					System.out.println("No, the student is not eligible to take: " + userInput);
				}
			}
			else if (userInput.equals("END")) {
				userDone = true;
			}
			else {
				System.out.println("error: " + userInput + " does not match the required format");
			}
		}
	}
	
	private void checkIfStudentCanTakeClass2() {		
		ArrayList<String> facts = wm.getFacts();
		
		Scanner input = new Scanner(System.in);
		String userInput = "";
		String regex = "[A-Z]{4}[0-9]{4}[H,K,L,U]?";
		boolean userDone = false;
		
		System.out.println("enter a class you wish to check eligibility for, type END to stop.");
		
		while (!userDone) {
			System.out.print(": ");
			userInput = input.next().trim().toUpperCase().replace(" ", "");
			if (userInput.matches(regex)) {
				ie.validateHypothesis3("Student can_take " + userInput);
			}
			else if (userInput.equals("END")) {
				userDone = true;
			}
			else {
				System.out.println("error: " + userInput + " does not match the required format");
			}
		}
	}
	
	
	
	public static void main(String[] args) {
		UI ui = new UI();
	}	
}